package com.nursinghome.user.controller;

import cn.hutool.core.date.DateUtil;
import com.nursinghome.annotation.Log;
import com.nursinghome.bean.Order;
import com.nursinghome.bean.OrderVo;
import com.nursinghome.bean.RespBean;
import com.nursinghome.common.service.OrderService;
import com.nursinghome.exception.NurisingHomeException;
import com.nursinghome.user.service.UserAccountService;
import com.nursinghome.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user/order")
public class UserOrderController {
    @Autowired
    private UserAccountService userAccountService;
    @Autowired
    private OrderService orderService;

    @GetMapping
    public RespBean selectAll(@RequestHeader(value = "token", required = false) String token) {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Long jwtEid =  new Long(String.valueOf(map.get("eid")));
        Order order = new Order();
        order.setEid(jwtEid);
        order.setDate(DateUtil.date());
        order.setStatus(3);
        List<Order> list = orderService.selectByCondition(order);
        return RespBean.ok("",list);
    }
    @GetMapping("/today")
    public RespBean selectToday(@RequestHeader(value = "token", required = false) String token){
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Long jwtEid =  new Long(String.valueOf(map.get("eid")));
        Order order = new Order();
        order.setEid(jwtEid);
        order.setDate(DateUtil.date());
        order.setStatus(1);
        List<Order> list = orderService.selectByCondition(order);
        return RespBean.ok("",list);
    }
    @PutMapping("/{aid}")
    public RespBean updateAccountElderly(@PathVariable("aid") Long aid) throws NurisingHomeException {
        userAccountService.updateAccountElderly(aid);

        return RespBean.ok("支付成功");
    }
    @GetMapping("/byaid")
    public RespBean selectByAidByUser(Long aid){

        return null;
    }
    //新增订单
    @Log(name = "预购菜品")
    @PostMapping
    public RespBean insert(@RequestHeader(value = "token", required = false) String token,
                           @RequestBody OrderVo orderVo) throws NurisingHomeException {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        long jwtEid = new Long(String.valueOf( map.get("eid")));
        orderVo.setEid(jwtEid);
        if (orderService.insert(orderVo)) {
            return RespBean.ok("新增成功");
        }
        return RespBean.error("新增失败");
    }
    //修改订单
    @PutMapping
    public RespBean update(@RequestBody Order order) throws NurisingHomeException {
        if (orderService.update(order)) {
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");
    }
    //删除订单
    @DeleteMapping("/{id}")
    public RespBean deleteById(@PathVariable("id") String id) throws NurisingHomeException {
        if (orderService.delete(id)) {
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }
    //回显订单数据
    @GetMapping("/{id}")
    public RespBean selectById(@PathVariable("id") String id) throws NurisingHomeException {
        Order order = orderService.selectById(id);
        return RespBean.ok("",order);
    }

    //根据老人id查询当天的所有订单
    @GetMapping("/{eid}")
    public RespBean selectByMid(@PathVariable("eid") Long eid){
        List<Order> orders = orderService.selectByCondition(
                new Order() {{
                    setEid(eid);
                    setDate(DateUtil.date());
                }}
        );
        return RespBean.ok("",orders) ;
    }
}